package dao.impl;

import dao.VoteDao;
import entity.Vote;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.JDBCUtils;

import java.util.List;

public class VoteDaoImpl implements VoteDao {
    private JdbcTemplate template= JDBCUtils.getJDBCTemplate();
    /**
     * 根据vName查询投票数据
     *
     * @param vName
     * @return
     */
    @Override
    public Vote selectVoteByvName(String vName) {
        String sql="select * from t_vote where v_name=?";
        List<Vote> query = template.query(sql, new BeanPropertyRowMapper<>(Vote.class), vName);
        return query.isEmpty()?null:query.get(0);
    }

    /**
     * 修改投票数据
     *
     * @param vote
     * @return
     */
    @Override
    public int updateVote(Vote vote) {
       String sql="update t_vote set v_num=v_num+1 where v_name=?";
        int n = template.update(sql, vote.getVName());
        return n;
    }
}
